package weaver.interfaces.solead.utils;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-11-26
 * @Description: ${description}
 * @ModifyLog:
 **/


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.nio.file.Paths;

/**
 * JSON对象与XML相互转换工具类
 *
 * @author Logan
 * @createDate 2019-02-12
 * @version 1.0.0
 *
 */
public class Json2XmlUtils {

    private static final String ENCODING = "UTF-8";

    /**
     * JSON对象转漂亮的xml字符串
     *
     * @param json JSON对象
     * @return 漂亮的xml字符串
     * @throws IOException
     * @throws SAXException
     */
    public static String jsonToPrettyXml(JSONObject json) throws IOException, SAXException {
        Document document = jsonToDocument( json );
        /* 格式化xml */
        OutputFormat format = OutputFormat.createPrettyPrint();
        // 设置缩进为4个空格
        format.setIndent( " " );
        format.setIndentSize( 4 );

        StringWriter formatXml = new StringWriter();
        XMLWriter writer = new XMLWriter( formatXml, format );
        writer.write( document );
        return formatXml.toString();
    }

    /**
     * JSON对象转漂亮的xml字符串
     *
     * @param array JSON对象
     * @return 漂亮的xml字符串
     * @throws IOException
     * @throws SAXException
     */
    public static String jsonToPrettyXml(JSONArray array) throws IOException, SAXException {
        StringWriter formatXml = new StringWriter();
        for(int i=0;i<array.size();i++){
            JSONObject json = array.getJSONObject( i );
            Document document = jsonToDocument( json );
            /* 格式化xml */
            OutputFormat format = OutputFormat.createPrettyPrint();
            // 设置缩进为4个空格
            format.setIndent( " " );
            format.setIndentSize( 4 );
            XMLWriter writer = new XMLWriter( formatXml, format );
            writer.write( document );
        }
        return formatXml.toString();
    }
    /**
     * JSON对象转xml字符串
     *
     * @param json JSON对象
     * @return xml字符串
     * @throws SAXException
     */
    public static String JsonToXml(JSONObject json) throws SAXException {
        return jsonToDocument( json ).asXML();
    }

    /**
     * JSON对象转Document对象
     *
     * @param json JSON对象
     * @return Document对象
     * @throws SAXException
     */
    public static Document jsonToDocument(JSONObject json) throws SAXException {
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding( ENCODING );

        // root对象只能有一个
        for (Object rootKey : json.keySet()) {
            Element root = jsonToElement( json.getJSONObject( (String) rootKey ), (String) rootKey );
            document.add( root );
            break;
        }
        return document;
    }

    /**
     * JSON对象转Element对象
     *
     * @param json     JSON对象
     * @param nodeName 节点名称
     * @return Element对象
     */
    public static Element jsonToElement(JSONObject json, String nodeName) {
        Element node = DocumentHelper.createElement( nodeName );
        for (Object key : json.keySet()) {
            Object child = json.get( key );
            if (child instanceof JSONObject) {
                node.add( jsonToElement( json.getJSONObject( (String) key ), (String) key ) );
            } else {
                Element element = DocumentHelper.createElement( (String) key );
                element.setText( json.getString( (String) key ) );
                node.add( element );
            }
        }

        return node;
    }

    /**
     * XML字符串转JSON对象
     *
     * @param xml xml字符串
     * @return JSON对象
     * @throws DocumentException
     */
    public static JSONObject xmlToJson(String xml) throws DocumentException {
        JSONObject json = new JSONObject();

        SAXReader reader = new SAXReader();
        Document document = reader.read( new StringReader( xml ) );
        Element root = document.getRootElement();

        json.put( root.getName(), elementToJson( root ) );

        return json;
    }

    /**
     * Element对象转JSON对象
     *
     * @param element Element对象
     * @return JSON对象
     */
    public static JSONObject elementToJson(Element element) {
        JSONObject json = new JSONObject();
        for (Object child : element.elements()) {
            Element e = (Element) child;
            if (e.elements().isEmpty()) {
                json.put( e.getName(), e.getText() );
            } else {
                json.put( e.getName(), elementToJson( e ) );
            }
        }

        return json;
    }

    /**
     * 字符串输出到文件
     *
     * @param str      字符串内容
     * @param filePath 文件路径
     * @throws IOException
     */
    public static void stringToFile(String str, String filePath) throws IOException {
        FileUtils.writeStringToFile( Paths.get( filePath ).toFile(), str, ENCODING );
    }

    /**
     * 字符串输出到文件
     *
     * @param str      字符串内容
     * @param filePath 文件路径
     * @throws IOException
     */
    public static void stringToFile(String str, URL filePath) throws IOException {
        FileUtils.writeStringToFile( new File( filePath.getPath() ), str, ENCODING );
    }

    /**
     * 字符串输出到文件
     *
     * @param str  字符串内容
     * @param file 输出文件
     * @throws IOException
     */
    public static void stringToFile(String str, File file) throws IOException {
        FileUtils.writeStringToFile( file, str, ENCODING );
    }

}